<!DOCTYPE html
    PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>产品列表</title>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script src="../../scripts/boot.js" type="text/javascript"></script>


</head>

<body>
    <style type="text/css">
        html,
        body {
            margin: 0;
            padding: 0;
            border: 0;
            width: 100%;
            height: 100%;
            overflow: hidden;
        }
    </style>
    <div class="mini-toolbar" style="padding:2px;border-bottom:0;">
        <table style="width:100%;">
            <tr>
                <td style="width:100%;">
                    <a class="mini-button" iconCls="icon-add" onclick="add()">新增</a>
                    <a class="mini-button" iconCls="icon-no" onclick="delSelected()" style="margin-left: 10px;">删除</a>


                </td>
                <td style="white-space:nowrap;"><label style="font-family:Verdana;"></label>
                    <input id="productName" class="mini-textbox" emptyText="请输入产品名称" width="120px" />
                    <input id="productUsage" class="mini-combobox" emptyText="请选择" data="productUsage"
                        showNullItem="true" width="130px" style="margin-left: 20px;" />
                    <a class="mini-button" iconCls="icon-find" style="margin-left: 20px;" onclick="onSearch()">查询</a>

                </td>
            </tr>
        </table>
    </div>
    <!--撑满页面-->
    <div class="mini-fit">

        <div id="productinfo" class="mini-datagrid" style="width:100%;height:100%;" url="../../productinfoaction" ajaxType="get"
          multiSelect="true" idField="id" allowResize="false"
         showPager="false" pageSize="10" >

            <div property="columns">
                <div type="checkcolumn"></div>
                <div field="productId" width="120" headerAlign="center" allowSort="true">产品编号</div>
                <div field="productName" width="120" headerAlign="center" allowSort="true">产品名称</div>
                <div field="country" width="100" headerAlign="center" renderer="onCountryRenderer">生产国家</div>
                <div field="language" width="100" headerAlign="center" allowSort="true">产品语言</div>
                <div field="productUsage" width="100" headerAlign="center" allowSort="true" renderer="onUsageRenderer">
                    产品用途</div>
                <div field="createTime" width="100" headerAlign="center" dateFormat="yyyy-MM-dd" allowSort="true">上市时间
                </div>
                <div field="quote" width="120" headerAlign="center" allowSort="true" dataType="currency">最新报价</div>
                <div field="count" width="120" headerAlign="center" allowSort="true">报价次数</div>

                <!-- 操作 -->
                <div name="action" width="120" headerAlign="center" renderer="onActionRenderer" cellStyle="padding:0;">
                    操作</div>
            </div>
        </div>

    </div>
    <div id="pager1" class="mini-pager" style="width:100%;background:#f0f3f7;border:solid 1px #ccc;" 
        onpagechanged="onPageChanged" sizeList="[5,10,20,50]" showPageSize="true" showPageIndex="true"
        showPageInfo="true" buttons="#buttons" >

    </div>

    <div id="buttons">
        <a style="margin-left: 300px;">@岗前培训结业模拟试题</a>
    </div>
    <script type="text/javascript">
        var productUsage = [
            { "id": "1", "text": "建筑工程" },
            { "id": "2", "text": "水利工程" },
            { "id": "3", "text": "机械工程" },
            { "id": "4", "text": "电力工程" },
            { "id": "5", "text": "其他用途" }
        ];
        var country = [
            { "id": "1", "text": "中国" },
            { "id": "2", "text": "美国" },
            { "id": "3", "text": "日本" },
            { "id": "4", "text": "德国" }
        ];

        mini.parse();
        var grid = mini.get("productinfo");
        grid.load();

        /* 自定义分页功能 */
        var pager = mini.get("pager1");
        // 数据加载完毕后加载分页总数
        grid.on("load", function (e) {
            pager.setTotalCount(grid.totalCount);
        });
        function onPageChanged(e) {
            pager.totalCount=grid.totalCount;
            grid.load({
                pageIndex: e.pageIndex,
                pageSize: e.pageSize,
                sortField: e.sortField,
                sortOrder: e.sortOrder
            });
            
        }
        // 操作按钮
        function onActionRenderer(e) {
            var grid = e.sender;
            var record = e.record;
            var uid = record._id;
            var rowIndex = e.rowIndex;

            var s = ' <a class="mini-button" href="javascript:editRow(\'' + uid + '\')"">编辑</a>'
                + '  <a class="mini-button" href="javascript:delRow(\'' + uid + '\')" >删除</a>';

            return s;
        }
        // 筛选功能
        function onSearch() {
            var productName = mini.get("productName").getValue();
            var productUsage = mini.get("productUsage").getValue();
            grid.load({
                productName: productName,
                productUsage: productUsage
            });
        }

        // 国家信息显示
        function onCountryRenderer(e) {
            for (var i = 0, l = country.length; i < l; i++) {
                var g = country[i];
                if (g.id == e.value) return g.text;
            }
            return "";
        }
        // 产品用途列表展示
        function onUsageRenderer(e) {
            for (var i = 0, l = productUsage.length; i < l; i++) {
                var g = productUsage[i];
                if (g.id == e.value) return g.text;
            }
            return "";
        }
        // 增加产品信息
        function add() {
            mini.open({
                url: "pages/productinfo/productinfoadd.html",
                title: "新增产品信息", width: 600, height: 400, allowResize: false,
                ondestroy: function (action) {

                    grid.reload();
                }
            });
        }

        // 删除当前记录
        function delRow(row_uid) {
            var row = grid.getRowByUID(row_uid);
            if (row) {
                mini.confirm("确定删除记录？", "确定？",
                    function (action) {
                        if (action == "ok") {
                            grid.loading("删除中，请稍后......");
                            $.ajax({
                                url: "../../productinfoaction",
                                type: "post",
                                data: {
                                    method: "del",
                                    data: row.productId
                                },
                                success: function (text) {
                                    var res = mini.decode(text);
                                    mini.alert(res.msg);
                                    grid.reload();
                                },
                                error: function () {
                                }
                            });
                        }
                    }
                );
            }
        }
        // 多选删除
        function delSelected() {
            var rows = grid.getSelecteds();
            if (rows.length > 0) {
                mini.confirm("确定删除记录？", "确定？",
                    function (action) {
                        if (action == "ok") {
                            var ids = [];
                            for (var i = 0, l = rows.length; i < l; i++) {
                                var r = rows[i];
                                ids.push(r.productId);
                            }
                            var id = ids.join(',');
                            grid.loading("操作中，请稍后......");
                            $.ajax({
                                url: "../../productinfoaction",
                                type: "post",
                                data: {
                                    method: "del",
                                    data: id
                                },
                                success: function (text) {
                                    var res = mini.decode(text);
                                    mini.alert(res.msg);
                                    grid.reload();
                                },
                                error: function () {
                                }
                            });
                        }
                    }
                );
            } else {
                mini.alert("请选中一条记录");
            }
        }
        // 修改产品信息表
        function editRow(row_uid) {
            var row = grid.getRowByUID(row_uid);
            if (row) {
                mini.open({
                    url: "pages/productinfo/productedit.html",
                    title: "修改产品信息", width: 600, height: 400,
                    onload: function () {
                        var iframe = this.getIFrameEl();
                        var data = { action: "edit", productId: row.productId };
                        iframe.contentWindow.SetData(data);

                    },
                    ondestroy: function (action) {

                        grid.reload();

                    }
                });

            } else {
                alert("请选中一条记录");
            }
        }

    </script>

</body>

</html>